#include <rtthread.h>
#include <dfs_fs.h>
#define DBG_TAG "sd_fs"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>


static void _sdcard_mount(void)
{
    rt_device_t device;
	
    device = rt_device_find("sd0");
    if (device == NULL)
    {
        device = rt_device_find("sd0");
    }
    if (device != RT_NULL)
    {
		
		rt_thread_mdelay(200);
        if (dfs_mount("sd0", "/", "elm", 0, 0) == RT_EOK)
        {
            LOG_I("sd card mount to '/'");
        }
        else
        {
			if(dfs_mkfs("elm","sd0") != 0)
			{
				rt_kprintf("sdcard format fail!\n");
			}
			if (dfs_mount("sd0", "/", "elm", 0, 0) == RT_EOK)
			{
				LOG_I("sd card mount to '/'");
				
			}
            else
				LOG_W("sd card mount to '/' failed!");
           
        }
    }
	
}

static void _sdcard_unmount(void)
{
    rt_thread_mdelay(200);
    dfs_unmount("/");
    LOG_I("Unmount \"/\"");

	mmcsd_wait_cd_changed(0);

}

static void sd_mount(void)
{
	rt_thread_mdelay(1000);
	_sdcard_mount();
	

}

int sd_mount_init(void)
{
	
	sd_mount();
	return RT_EOK;
}
INIT_APP_EXPORT(sd_mount_init);